﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace WriteLog
{
    /// <summary>
    /// 通过此类可以记录日志
    /// </summary>
    public static class Log
    {
        #region 私有内容  Writer
        private static ISaveLog _iSaveLog;

        private static ISaveLog Writer
        {
            get 
            {
                try
                {
                    if (_iSaveLog == null)
                        _iSaveLog = (ConfigurationManager.GetSection("LogControl") as LogControl).LogWriter;
                    return _iSaveLog;
                }
                catch
                {
                    return new NullSaveLog();
                }
            }
        }
        #endregion

        #region 记录普通日志
        /// <summary>
        /// 记录普通日志
        /// </summary>
        /// <param name="Message">日志内容</param>
        public static void Normal(string Message)
        {
            Writer.Save(new NormalLogMessage(Message));
        }
        /// <summary>
        /// 记录普通日志
        /// </summary>
        /// <param name="provider">日志来源</param>
        /// <param name="eventID">产生日志的ID</param>
        /// <param name="taskName">产生日志的任务</param>
        /// <param name="messageBody">日志内容</param>
        public static void Normal(string provider, int eventID, string taskName, string messageBody)
        {
            Writer.Save(new NormalLogMessage(provider, eventID, taskName, messageBody));
        }

        #endregion

        #region 记录控制台输出消息为日志
        /// <summary>
        /// 记录控制台输出内容为日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void Console(string message)
        {
            Writer.Save(new ConsoleWriteLogMessage(message));
        }
        #endregion

        #region 记录信息日志
        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="Message">日志内容</param>
        public static void Messages(string Message)
        {
            Writer.Save(new Message(Message));
        }
        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="provider">日志来源</param>
        /// <param name="eventID">产生日志的ID</param>
        /// <param name="taskName">产生日志的任务</param>
        /// <param name="messageBody">日志内容</param>
        public static void Messages(string provider, int eventID, string taskName, string messageBody)
        {
            Writer.Save(new Message(provider, eventID, taskName, messageBody));
        }

        #endregion

        #region 记录错误日志
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void Bug(string message)
        {
            Writer.Save(new BugMessage(message));
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="provider">日志来源</param>
        /// <param name="eventID">产生日志的ID</param>
        /// <param name="taskName">产生日志的任务</param>
        /// <param name="messageBody">日志内容</param>
        public static void Bug(string provider, int eventID, string taskName, string messageBody)
        {
            Writer.Save(new BugMessage(provider, eventID, taskName, messageBody));
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="ex">产生错误的异常信息</param>
        public static void Bug(Exception ex)
        {
            Writer.Save(new BugMessage(ex));
        }
        #endregion

        #region 记录警告信息
        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="message">记录警告信息内容</param>
        public static void Warning(string message)
        {
            Warning("NoApplicationName", 0, "无", message);
        }
        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="provider">日志来源</param>
        /// <param name="eventID">产生日志的ID</param>
        /// <param name="taskName">产生日志的任务</param>
        /// <param name="messageBody">日志内容</param>
        public static void Warning(string provider, int eventID, string taskName, string messageBody)
        {
            BaseLogMessage blm = new BaseLogMessage();
            blm.Level = LogLevel.Warning;
            blm.Provider = provider;
            blm.EventID = eventID;
            blm.TaskName = taskName;
            blm.MessageBody = messageBody;

            Writer.Save(blm);
        }
        #endregion

        #region 记录认证信息
        /// <summary>
        /// 记录认证信息
        /// </summary>
        /// <param name="message">记录认证信息内容</param>
        public static void Auth(string message)
        {
            Auth("NoApplicationName", 0, "无", message);
        }
        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="provider">日志来源</param>
        /// <param name="eventID">产生日志的ID</param>
        /// <param name="taskName">产生日志的任务</param>
        /// <param name="messageBody">日志内容</param>
        public static void Auth(string provider, int eventID, string taskName, string messageBody)
        {
            BaseLogMessage blm = new BaseLogMessage();
            blm.Level = LogLevel.Auth;
            blm.Provider = provider;
            blm.EventID = eventID;
            blm.TaskName = taskName;
            blm.MessageBody = messageBody;

            Writer.Save(blm);
        }
        #endregion

        #region 记录认证失败信息
        /// <summary>
        /// 记录认证失败信息
        /// </summary>
        /// <param name="message">记录认证失败信息</param>
        public static void AuthWrong(string message)
        {
            AuthWrong("NoApplicationName", 0, "无", message);
        }
        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="provider">日志来源</param>
        /// <param name="eventID">产生日志的ID</param>
        /// <param name="taskName">产生日志的任务</param>
        /// <param name="messageBody">日志内容</param>
        public static void AuthWrong(string provider, int eventID, string taskName, string messageBody)
        {
            BaseLogMessage blm = new BaseLogMessage();
            blm.Level = LogLevel.AuthWrong;
            blm.Provider = provider;
            blm.EventID = eventID;
            blm.TaskName = taskName;
            blm.MessageBody = messageBody;

            Writer.Save(blm);
        }
        #endregion

        #region 记录自定义日志
        /// <summary>
        /// 记录自定义日志
        /// </summary>
        /// <param name="blm">记录自定义日志内容</param>
        public static void BaseLog(BaseLogMessage blm)
        {
            if (blm != null)
                Writer.Save(blm);
        }
        #endregion
    }
}
